//
//  ViewController.m
//  导航栏
//
//  Created by 小果 on 16/5/13.
//  Copyright © 2016年 小果. All rights reserved.
//

#import "ViewController.h"
#import "TestViewController.h"
#import "XGTableViewCell.h"

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
// 自定义导航栏
@property (nonatomic, weak) UIView *nav;
// 导航栏上的文字
@property (nonatomic, weak) UILabel *lab;
// tableView
@property (nonatomic, weak) UITableView *tab;
// tbaleView头部的图像
@property (nonatomic, weak) UIImageView *img;
// 滚动停止后的偏移量
@property (nonatomic, assign) CGFloat lastOffset;
// 用于保存frame
@property (nonatomic,assign)CGRect BRect;
// 传参是用的
@property (nonatomic, copy) NSString *str;
// 用来判断回到顶部按钮是用
@property (nonatomic, assign) CGFloat lastOffsetY;
// 存放图片的数组
@property (nonatomic, strong) NSArray *imageArr;
// 回到顶部按钮
@property (nonatomic, weak) UIButton *btnTop;
// 四个悬浮按钮
@property (nonatomic, weak) UIButton *btn0;
@property (nonatomic, weak) UIButton *btn1;
@property (nonatomic, weak) UIButton *btn2;
@property (nonatomic, weak) UIButton *btn3;
// 记录按钮的点击结果
@property (nonatomic,assign) BOOL relust;
@end

@implementation ViewController
-(NSArray *)imageArr{
    if (nil == _imageArr){
        
        NSString *path = [[NSBundle mainBundle] pathForResource:@"image.plist" ofType:nil];
        _imageArr = [NSArray arrayWithContentsOfFile:path];
        
    }
    return _imageArr;
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    
    UIButton *btnTop = [[UIButton alloc] init];
    CGFloat btnTopW = 40;
    CGFloat btnTopH = btnTopW;
    CGFloat btnTopX = ScreenW - 10 - btnTopW;
    CGFloat btnTopY = ScreenH - 50 - btnTopH;
    btnTop.frame = CGRectMake(btnTopX, btnTopY, btnTopW, btnTopH);
    btnTop.backgroundColor = [UIColor redColor];
    [btnTop setTitle:@"顶部" forState:UIControlStateNormal];
    [btnTop addTarget:self action:@selector(backToTop) forControlEvents:UIControlEventTouchUpInside];
    btnTop.hidden = YES;
    btnTop.layer.cornerRadius = 20;
    btnTop.layer.masksToBounds = YES;
    self.btnTop = btnTop;
    [self.view addSubview:btnTop];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 创建tableView
    [self createTableView];
    
    // 自定义导航栏
    [self setupCustomNavigationBar];
    
    // 设置表格头部
    [self setupTableViewHeader];
    
    // 加载悬浮按钮和动画
    [self setupBtnWithAnim];
    
}

#pragma mark - 自定义导航栏
-(void)setupCustomNavigationBar{
    
    // 导航栏的位置
    UIView *nave = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, NavHeight)];
    self.nav = nave;
    nave.backgroundColor = BgColor;
    [self.view addSubview:nave];
    // 导航栏上的文字
    UILabel *lab = [[UILabel alloc] init];
    lab.text = @"我们都是好孩子";
    lab.font = [UIFont boldSystemFontOfSize:18];
    lab.textColor = [UIColor redColor];
    CGSize labSize = [lab.text sizeWithFont:[UIFont boldSystemFontOfSize:18] maxSize:CGSizeMake(MAXFLOAT, MAXFLOAT) ];
    
    CGFloat labX = (ScreenW - labSize.width) * 0.5;
    CGFloat labY = 20 + (44 - labSize.height) * 0.5;
    lab.frame = CGRectMake(labX, labY, labSize.width, labSize.height);
    self.lab = lab;
    [nave addSubview:lab];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeContactAdd];
    [btn addTarget:self action:@selector(customBtnClick) forControlEvents:UIControlEventTouchUpInside];
    btn.x = ScreenW - 30;
    btn.y = 30;
    [nave addSubview:btn];
    
}

#pragma mark - 按钮点击
-(void)customBtnClick{
    TestViewController *test = [[TestViewController alloc] init];
    UINavigationController *navc = [[UINavigationController alloc] initWithRootViewController:test];
    test.str = self.lab.text;
    [self presentViewController:navc animated:YES completion:nil];
}

#pragma mark - 创建TableView
-(void)createTableView{
    UITableView *tab = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, ScreenW, ScreenH - 64)];
    
    tab.delegate = self;
    tab.dataSource = self;
    tab.backgroundColor = BgColor;
    tab.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tab = tab;
    tab.scrollsToTop = NO;
    [self.view addSubview:tab];

}

#pragma mark - 设置表格头部
-(void)setupTableViewHeader{
    // 表格的头部
    UIView *head = [[UIView alloc] initWithFrame:CGRectMake(0, 0,ScreenW,120)];
    head.backgroundColor = BgColor;
    self.tab.tableHeaderView = head;
    
    // 中间的头像
    UIImageView *img = [[UIImageView alloc] init];
    CGFloat imgW = ImgW;
    CGFloat imgH = ImgW;
    CGFloat imgX = (ScreenW - imgW) * 0.5;
    CGFloat imgY = NavHeight + (head.bounds.size.height - imgH) * 0.5;

    img.image = [UIImage imageNamed:@"11.jpg"];
    img.userInteractionEnabled = NO;
    
    CGRect rect = CGRectMake(imgX, imgY, imgW, imgH);
    img.frame = rect;
    self.BRect = rect;
    img.layer.cornerRadius = ImgW * 0.5;
    img.clipsToBounds = YES;
    img.layer.borderWidth = 2;
    img.layer.borderColor = [UIColor yellowColor].CGColor;

    self.img = img;
    [self.view addSubview:img];

}
#pragma mark - 视图在滚动的时候对头像的frame进行改变
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    CGFloat a = scrollView.contentOffset.y;
    CGRect r = self.BRect;
    
    r.origin.y = r.origin.y - a;
    self.img.frame = r;
    
    self.lastOffset = scrollView.contentOffset.y;
    
    NSLog(@"%f",self.lastOffset);
    
    if(self.lastOffset > 0){
        
        __block CGRect imgF = self.img.frame;
        [self.img removeFromSuperview];
        [self.view addSubview:self.img];
        //利用20作为临界点   在 0 ---20 范围内进行 缩放
        //大于20 的时候  w,h 为  36
        //0 的时候       w,h 为100
        //在 0 --20 改变的总数为  64,根据比例计算某一点改变的大小
        if (self.lastOffset > 0 && self.lastOffset <= 20) {
            CGFloat w = 100 - 64 * (self.lastOffset / 20.0);
            imgF.size.width = w;
            imgF.size.height = w;
            self.img.layer.cornerRadius = w / 2;
        }
        
        if (self.lastOffset > 20) {
            imgF.size.width = 36;
            imgF.size.height = 36;
             self.img.layer.cornerRadius = 18;
        }
        // 当图像的Y值和文字的相对Y值一致时，对图像的Y值进行一次性赋值
        if (imgF.origin.y <= 25){
            imgF.origin.y = 25;
        }
        
        imgF.origin.x = (ScreenW - imgF.size.width - self.lab.frame.size.width) * 0.5;
        NSLog(@"btnF.origin.x:%f",imgF.origin.x);
    
         self.img.frame = imgF;
        
        self.img.clipsToBounds = YES;
        
        CGRect labF = self.lab.frame;
        // 图片到导航栏时label的X值
        labF.origin.x = ScreenW - CGRectGetMaxX(imgF) - labF.size.width+ imgF.size.width + 5;
        self.lab.frame = labF;
    }
    
    if(self.lastOffset <= 0) {
        self.img.layer.cornerRadius = ImgW * 0.5;
        self.img.frame = self.BRect;
        self.img.center = self.tab.tableHeaderView.center;
        [self.tab.tableHeaderView addSubview:self.img];
        CGRect labF = self.lab.frame;
        labF.origin.x = (ScreenW - self.lab.frame.size.width) * 0.5;
        self.lab.frame = labF;
    }
    // 计算tableView滚动的偏移量
    self.lastOffsetY = scrollView.contentOffset.y;
    if (self.lastOffsetY >= self.tab.frame.size.height){
        self.btnTop.hidden = NO;
       
    }else{
        self.btnTop.hidden = YES;
    }
    
}
#pragma mark - 回到顶部
-(void)backToTop{
    
    [self.tab setContentOffset:CGPointMake(0, 0) animated:YES];
    
}

#pragma mark - tableVeiw的数据源方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.imageArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *ID = @"cell";
    XGTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (nil == cell){
        cell = [[XGTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
    }
    cell.imageview.image = [UIImage imageNamed:self.imageArr[indexPath.row][@"icon"]];
    cell.lab.textColor = [UIColor redColor];
    cell.lab.font = [UIFont boldSystemFontOfSize:18];
    cell.lab.text = [NSString stringWithFormat:@"测试数据-----%zd",indexPath.row];
    
    
    return cell;
}

#pragma mark - 每一行的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [XGTableViewCell getHeight] + 10;
}

#pragma mark - 左下角的悬浮按钮的设置
-(void)setupBtnWithAnim{
    
    UIButton *btn3 = [[UIButton alloc] init];
    btn3.backgroundColor = [UIColor brownColor];
    [self setupBtn:btn3];
    [btn3 setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [btn3 setTitle:@"嘿嘿" forState:UIControlStateNormal];
    [btn3 addTarget:self action:@selector(customBtnClick) forControlEvents:UIControlEventTouchUpInside];
    self.btn3 = btn3;
    [self.view addSubview:btn3];
    
    UIButton *btn2 = [[UIButton alloc] init];
    btn2.backgroundColor = [UIColor purpleColor];
    [self setupBtn:btn2];
    [btn2 setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];
    [btn2 setTitle:@"呵呵" forState:UIControlStateNormal];
    [btn2 addTarget:self action:@selector(customBtnClick) forControlEvents:UIControlEventTouchUpInside];
    self.btn2 = btn2;
    [self.view addSubview:btn2];
    
    UIButton *btn1 = [[UIButton alloc] init];
    btn1.backgroundColor = [UIColor redColor];
    [self setupBtn:btn1];
    [btn1 setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
    [btn1 setTitle:@"哈哈" forState:UIControlStateNormal];
    [btn1 addTarget:self action:@selector(customBtnClick) forControlEvents:UIControlEventTouchUpInside];
    self.btn1 = btn1;
    [self.view addSubview:btn1];
    
    UIButton *btn0  = [[UIButton alloc] init];
    btn0.backgroundColor = [UIColor blueColor];
    [self setupBtn:btn0];
    [btn0 setTitle:@"敢点吗" forState:UIControlStateNormal];
    [btn0 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    self.btn0 = btn0;
    [btn0 addTarget:self action:@selector(btnWithAnimation) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn0];
    
}
#pragma mark - 悬浮按钮的动画
-(void)btnWithAnimation{
    
    if (self.relust) {
        
        [UIView animateWithDuration:0.3 animations:^{
            
            self.btn1.center = self.btn0.center;
            self.btn2.center = self.btn0.center;
            self.btn3.center = self.btn0.center;
            
        }];
        self.relust = !self.relust;
        return;
        
    }else{
        
        [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:0.4 initialSpringVelocity:10.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            
            CGRect b1Frame = self.btn1.frame;
            b1Frame.origin.x = 5;
            b1Frame.origin.y = ScreenH - 220;
            self.btn1.frame = b1Frame;
            
            CGRect b2Frame = self.btn2.frame;
            b2Frame.origin.x = CGRectGetMaxX(self.btn0.frame) + 10;
            b2Frame.origin.y = self.btn0.frame.origin.y - 70;
            self.btn2.frame = b2Frame;
            
            CGRect b3Frame = self.btn3.frame;
            b3Frame.origin.x = CGRectGetMaxX(self.btn0.frame) + 35;
            b3Frame.origin.y = ScreenH - BtnW - 5;
            self.btn3.frame = b3Frame;
            
        }completion:^(BOOL finished) {
            
            self.relust = !self.relust;
            
        }];
        
    }
    
}
#pragma mark - 抽取共有的属性
-(void)setupBtn:(UIButton *)sender{
    sender.frame = CGRectMake(10, ScreenH - 100, BtnW, BtnW);
    sender.layer.cornerRadius = BtnW * 0.5;
    sender.layer.masksToBounds = YES;
    sender.titleLabel.font = [UIFont boldSystemFontOfSize:18];
    
}
@end
